package com.bidevalution.business.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bidevalution.business.service.BeLayoutConfigService;
import com.bidevalution.business.service.BeMethodLayoutService;
import com.bidevalution.business.service.BeMethodStepService;
import com.bidevalution.dao.entity.BeLayoutConfig;
import com.bidevalution.dao.entity.BeMethodColumn;
import com.bidevalution.dao.entity.BeMethodLayout;
import com.bidevalution.dao.entity.BeMethodLayoutKey;
import com.bidevalution.dao.mapper.BeMethodColumnMapper;
import com.bidevalution.dao.mapper.BeMethodLayoutMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.IdsUtil;

@Service
public class BeMethodLayoutServiceImpl implements BeMethodLayoutService {
	@Autowired
	private BeMethodLayoutMapper beMethodLayoutMapper;
	@Autowired
	private BeMethodColumnMapper beMethodColumnMapper;
	@Autowired
	private BeLayoutConfigService beLayoutConfigService;
	@Autowired
	private BeMethodStepService beMethodStepService;

	@Override
	@Transactional
	public void save(BeMethodLayout beMethodLayout, String config) {
		BeMethodLayoutKey key = new BeMethodLayoutKey();
		key.setLayoutCode(beMethodLayout.getLayoutCode());
		key.setMethodStepId(beMethodLayout.getMethodStepId());
		//查询页面模板配置表
		
		if (beMethodLayoutMapper.selectByPrimaryKey(key) != null) {
			beMethodLayoutMapper.updateByPrimaryKey(beMethodLayout);
		} else {
			BeLayoutConfig beLayoutConfig = beLayoutConfigService.selectByPrimaryKey(
					beMethodStepService.selectById(beMethodLayout.getMethodStepId()).getLayoutConfigId());
			//判断是否存在默认在行单击事件
			if (!StringUtils.isEmpty(beLayoutConfig.getClickFunction())) {
				//将json内容转换成map
				Map<String, Object> clickFunctionMap = GsonUtil.jsonToMapWithGson(beLayoutConfig.getClickFunction());
				//判断是否不为空
				if (!StringUtils.isEmpty(clickFunctionMap.get(beMethodLayout.getLayoutCode()))) {
					beMethodLayout.setClickFunction(clickFunctionMap.get(beMethodLayout.getLayoutCode()).toString());
				}
			}
			beMethodLayoutMapper.insert(beMethodLayout);
		}

		if (!StringUtils.isEmpty(config)) {
			BeMethodColumn beMethodColumn;
			int i = 1;
			List<String> containList = Lists.newArrayList();
			List<BeMethodColumn> dataList = beMethodColumnMapper.selectListByStepId(beMethodLayout.getMethodStepId(),
					beMethodLayout.getLayoutCode());
			List<BeMethodColumn> beMethodColumnList = Lists.newArrayList();
			Map<String, String> columnCodeMap = Maps.newHashMap();
			for (BeMethodColumn methodColumn : dataList) {
				columnCodeMap.put(methodColumn.getColumnCode(), methodColumn.getSeqId());
			}
			for (Map<String, Object> dataMap : GsonUtil.jsonToListMapWithGson(config)) {
				beMethodColumn = new BeMethodColumn();
				beMethodColumn.setClickFunction(
						dataMap.get("clickFunction") == null ? "" : dataMap.get("clickFunction").toString());
				beMethodColumn
						.setValidator(dataMap.get("validator") == null ? "" : dataMap.get("validator").toString());
				beMethodColumn
						.setEnableEdit(dataMap.get("enableEdit") == null ? "" : dataMap.get("enableEdit").toString());
				beMethodColumn
						.setColumnCode(dataMap.get("columnCode") == null ? "" : dataMap.get("columnCode").toString());
				beMethodColumn
						.setColumnText(dataMap.get("columnText") == null ? "" : dataMap.get("columnText").toString());
				beMethodColumn.setCreateTime(new Date());
				beMethodColumn.setColumnWidth(dataMap.get("columnWidth") == null ? "" : dataMap.get("columnWidth").toString());
				beMethodColumn.setDefaultValue(
						dataMap.get("defaultValue") == null ? "" : dataMap.get("defaultValue").toString());
				beMethodColumn
						.setInputType(dataMap.get("inputType") == null ? "" : dataMap.get("inputType").toString());
				beMethodColumn.setColumnDescription(
						dataMap.get("columnDescription") == null ? "" : dataMap.get("columnDescription").toString());
				beMethodColumn.setFormulaConfig(
						dataMap.get("formulaConfig") == null ? "" : dataMap.get("formulaConfig").toString());
				beMethodColumn.setColumnConfig(
						dataMap.get("columnConfig") == null ? "" : dataMap.get("columnConfig").toString());
				beMethodColumn.setPermissionConfig(
						dataMap.get("permissionConfig") == null ? "" : dataMap.get("permissionConfig").toString());
				beMethodColumn.setLayoutCode(beMethodLayout.getLayoutCode());
				beMethodColumn.setMethodStepId(beMethodLayout.getMethodStepId());
				beMethodColumn.setSeqId(columnCodeMap.get(beMethodColumn.getColumnCode()));
				beMethodColumn.setUpdateTime(new Date());
				beMethodColumn.setOrderNum(i++);
				if(StringUtils.isEmpty(dataMap.get("restrictTip"))) {
					beMethodColumn.setRestrictTip("0");
				}else {
					beMethodColumn.setRestrictTip(dataMap.get("restrictTip").toString());
				}
				beMethodColumnList.add(beMethodColumn);
				containList.add(beMethodColumn.getColumnCode());
			}
			// 删除不存在在字段
			for (BeMethodColumn methodColumn : dataList) {
				if (!containList.contains(methodColumn.getColumnCode())) {
					beMethodColumnMapper.deleteByPrimaryKey(methodColumn.getSeqId());
				}
			}
			// 新增或者删除
			for (BeMethodColumn methodColumn : beMethodColumnList) {
				if (StringUtils.isEmpty(methodColumn.getSeqId())) {
					methodColumn.setSeqId(IdsUtil.uuid());
					beMethodColumnMapper.insert(methodColumn);
				} else {
					beMethodColumnMapper.updateByPrimaryKey(methodColumn);
				}
			}

		}
	}

	@Override
	public BeMethodLayout selectById(String methodStepId, String layoutCode) {
		BeMethodLayoutKey beMethodLayoutKey = new BeMethodLayoutKey();
		beMethodLayoutKey.setLayoutCode(layoutCode);
		beMethodLayoutKey.setMethodStepId(methodStepId);
		BeMethodLayout beMethodLayout = beMethodLayoutMapper.selectByPrimaryKey(beMethodLayoutKey);
		//为空则查询页面配置是否存在
		if(beMethodLayout == null) {
			beMethodLayout = new BeMethodLayout();
			beMethodLayout.setLayoutCode(layoutCode);
			beMethodLayout.setMethodStepId(methodStepId);
		}
		if(StringUtils.isEmpty(beMethodLayout.getClickFunction())) {
			System.out.println(beMethodStepService.selectById(beMethodLayout.getMethodStepId()).getLayoutConfigId());
			BeLayoutConfig beLayoutConfig = beLayoutConfigService.selectByPrimaryKey(
					beMethodStepService.selectById(beMethodLayout.getMethodStepId()).getLayoutConfigId());
			//判断是否存在默认在行单击事件
			if (!StringUtils.isEmpty(beLayoutConfig.getClickFunction())) {
				//将json内容转换成map
				Map<String, Object> clickFunctionMap = GsonUtil.jsonToMapWithGson(beLayoutConfig.getClickFunction());
				//判断是否不为空
				if (!StringUtils.isEmpty(clickFunctionMap.get(beMethodLayout.getLayoutCode()))) {
					beMethodLayout.setClickFunction(clickFunctionMap.get(beMethodLayout.getLayoutCode()).toString());
				}
			}
		}
		return beMethodLayout;
	}

}
